import axios from 'axios'
import store from '@/store'
// import router from '@/router'
import { Message } from 'element-ui'

const request = axios.create({
  baseURL: 'https://slwl-api.itheima.net',
  timeout: 5000
})

// 请求拦截器
request.interceptors.request.use((config) => {
  const token = store.state.user.token
  if (token) {
    config.headers.Authorization = `Bearer ${token}`
  }
  return config
},
(err) => {
  return Promise.reject(err)
})

// 响应拦截器
request.interceptors.response.use(
  (response) => {
    return response.data
  },
  (err) => {
    // console.dir(err)
    // if (err.response.status === 401) {
    //   Message.warning('登录过期，请重新登录')
    //   router.push('/login')
    // } else {
    // }
    Message.error(err.message)
    return Promise.reject(err)
  }
)

export default request
